<#

Usage:

This script should be dot-sourced, i.e.  run with a ". " in front of it, i.e.

. .\Bootstrapper.ps1

Short Version:  

This bootstrapper loads up all the libraries included in this distribution.  

Longer Version

This bootstrapper figures out where he lives, finds the "scripts" folder, 
runs all of the .ps1 scripts in it (and its subfolders) in alphabetical order.  

Additionally, it calls InitLib in order to hook into some runtime loading and 
initialization (contained in 0-Core\0-Startup) that I added so that I don't 
have to worry about putting things in the right sequence based on their 
dependencies on one another.

If one of the scripts is causing a problem, delete it or change its extension to exclude it.

It also provides a function called Reload-Boostrapper that can be called after adding or editing script files.

When the .ps1 scripts are executed, they are dot-sourced, meaning that any variables created
will reside in the current context.  This is how we "load" the functions in the libraries.  
Generally, we these scripts should not do anything except declare functions.

And, yes, I'm eventually going to refactor this to a PowerShell 2.0 script module.  Eventually.


#>


if(!$BootstrapperPath){
    $BootstrapperPath = $MyInvocation.MyCommand.Path
    write-host "Set Bootstrapper path to $global:BootstrapperPath"
}
if(!$ScriptDir) {
    $ScriptDir = (split-path $MyInvocation.MyCommand.Path -Parent)
}

function Reload-Bootstrapper {
    Log "Running  $BootstrapperPath"
	. "$BootstrapperPath"
}


function Load-PSLibraries {
    cd $ScriptDir
	
        Push-Location 'scripts'        
        
            # Initialize the dependency loader in order to load everything else
    		. 'PowerShell\PowerShell.DependencyLoader.ps1'
    		Initialize-DependencyLoader
        
            $libs = ls -r -include *.ps1
            $private:i = 0;
            $private:activity = "Loading script Files";
        	$libs | % { 
                $i++
                Write-Progress -Activity $activity -Status $_ -PercentComplete (100*$i/$libs.Count) 
                try { Log-Verbose "Importing $_" } catch {}
                . $_.FullName  
            }
            Write-Progress -Activity $activity -Completed -Status "All done."
            Log "Done loading scripts"
          
        Pop-Location
        Push-Location lib
            $dlls = ls -r -include *.dll
        Pop-Location
    
    Init-Lib -AllLibs
    Log "Done initializing libs"
    if($dlls){
        $dlls | % {
            Import-Assembly -filename:($_.FullName)
        }
    }
}

. Load-PSLibraries

Log "Bootstrapper Loaded"

